<script setup>
import { computed, onMounted, ref } from 'vue'
import { ElLoading, ElMessage} from 'element-plus'

import commentApi from '@/api/comment.js';

const props = defineProps({
    type: {
        type: String,
        require: true,
        default: 'tag'
    },
    title: {
        type: String,
        require: true,
        default: '评论'
    }
})

let comment = ref('');
let comments = ref([]);

let currentPage = ref(1);
let pageSize = ref(5);
let total = ref(0);

let hideOnSinglePage = ref(true);
let showPlaceholder = ref(true);

let disabled = computed(() => !comment.value.trim());

let submitComment = () => {
    const loading = ElLoading.service({
        target: '.comment-input',
        lock: true,
        text: 'Loading',
		background: 'rgba(0, 0, 0, 0.01)'
    })
    // 提交评论
    commentApi.addComment(comment.value, new Date().toLocaleDateString(), props.type).then(res => {
        setTimeout(() => {
            loading.close();
            if (res.code === 200) {
                comment.value = '';
                ElMessage.success(res.message)
                handleCurrentChange(1);
            } else {
                ElMessage.error(res.message)
            }
        }, 2000);
    }).catch(e => {
        loading.close();
        ElMessage.error(e.message)
    })    
}

onMounted(() => {
    showPlaceholder.value = true;
    commentApi.getPages(currentPage.value, pageSize.value, props.type).then((result) => {
        setTimeout(() => {
            console.log(`当前页: ${currentPage.value}，分页数据`, result);
            showPlaceholder.value = false;
            total.value = result.data.total;
            comments.value = result.data.list;
        }, 2000)
    }).catch(e => {
        console.log(e);
        showPlaceholder.value = false;
        ElMessage.error(e.message)
    })
})

const handleCurrentChange = (val) => {
    showPlaceholder.value = false;
    const loading = ElLoading.service({
        target: '.comments',
        text: 'Loading',
		background: 'rgba(0, 0, 0, 0.01)'
    })
    commentApi.getPages(val, pageSize.value).then((result) => {
        setTimeout(() => {
            loading.close();
            console.log(`当前页: ${val}，分页数据`, result);
            total.value = result.data.total;
            currentPage.value = val;
            comments.value = result.data.list;
        }, 2000)
    }).catch(e => {
        console.log(e);
        loading.close();
        ElMessage.error(e.message)
  })
}
let loading = ref(true)
</script>

<template>
    <div class="comment-wrapper">
        <div class="comment-header">
            <span class="comment-header-title">{{ props.title }}</span>
            <span class="comment-header-count">({{ comments.length }})</span>
        </div>
        <div class="comment-input">
            <textarea v-model="comment" placeholder="请输入评论..." rows="4"></textarea>
            <button @click="submitComment" :disabled >提交</button>
            <div class="comments">
                <div v-for="(item, index) in comments" :key="index" class="comment-item">
                    <strong>{{ item.author }}:</strong> {{ item.content }} <span class="comment-item-date">({{ item.date }})</span>
                </div>
                <div v-show="showPlaceholder">
                    <el-skeleton :rows="5" animated />
                </div>
            </div>
            <el-pagination 
            class="pagination"
            color 
            layout="prev, pager, next" 
            v-model:current-page="currentPage"
            :total="total" 
            :pager-count="5"
            :hide-on-single-page="hideOnSinglePage"
            @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<style lang="scss" scoped>

.comment-wrapper {
    text-align: left;
    padding: 0 10px;

    .comment-header {
        display: flex;
        align-items: center;
        font-size: 18px;
        margin-bottom: 10px;
        .comment-header-count {
            color: $text-color-777;
            font-size: 16px;
        }
    }
    .comment-input {
        display: flex;
        flex-direction: column;
        gap: 10px;
        width: 100%;
        min-width: 60px;
        margin: 0 auto;
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            resize: none;
        }

        button {
            padding: 10px 20px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            &:disabled {
                opacity: 0.5;
            }
        }
        .comments {
            margin-top: 10px;
            .comment-item {
                padding: 10px 0;
                border-bottom: 1px solid var(--divider-color);
                strong {
                    font-size: 16px;
                    color: var(--primary-color);
                }
                .comment-item-date {
                    color: #999;
                    font-size: 0.9rem;
                    margin-left: 10px;
                }
            }
            .comment-item:last-child {
                border-bottom: none;
            }
        }
        .pagination {
            margin-top: 20px;
            justify-content: center;
        }
    }
}

</style>